function init(){
	var page = 0;
	var rows = 10;
	$(".select-box ul").on('click','li',function(){
	    $(this).parent().siblings('.show').val($(this).html());
	    $(this).parent().siblings('.hidden').val($(this).attr('data-id'));
	});
	$(".select-box div.fl").click(function(e){
	    e.stopPropagation();
	    var div=$(this);
	    div.children('ul').toggleClass('hide');
	    $(document).click(function() {
	        if (!div.children('ul').hasClass("hide")) {
	            div.children('ul').addClass("hide");
	        }
	    });
	});
	
	/**
	 * 订单号查询的方法
	 */
	$('.search-box').on('click','span',function(){
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2], conditions[3], conditions[4]);
	});
	
	/**
	 * 仓库查询的方法
	 */
	$('#warehouseSearchBox').on('click','li',function(){
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2], conditions[3], conditions[4]);
	});
	
	/**
	 * 发货时间查询的方法
	 */
	$('#datePicker').on('change',function(e){
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2], conditions[3], conditions[4]);
	});
	
	/**
	 * 根据状态查询的方法
	 */
	$('.top-block ul li').click(function(){
		$(this).addClass('active').siblings('li').removeClass('active');
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2], conditions[3], conditions[4]);
	});
	//w调用日期选择插件
	$('#datePicker').daterangepicker();
	$('.search-box span').click(function(){
		var name=$(this).siblings('input').val();
	});
	loadSearchBox();
	loadComment(page,rows,'','','','','');
}

/**
 * 获取所有的查询条件
 */
function findSearchConditions(){
	var strs = new Array();
	$('#type').children('li').each(function(){
		if($(this).attr('class') == 'active'){
			strs[0] = $(this).attr('data-id');
		}
	});
	strs[1] = $('#orderId').val();
	strs[2] = $('#warehouseId').val();
	var deliveryDate = $('#datePicker').val();
	var deliveryDates = deliveryDate.split(' - ');
	strs[3] = deliveryDates[0];
	strs[4] = deliveryDates[1];
	return strs;
}
/**
 * 加载选择搜索框数据
 * @param page
 * @param rows
 */
function loadSearchBox() {
	/*
	 * 仓库选择框赋值
	 */
	$.ajax({
		type: "post",
		url: '/diconet/warehouse/warehouse/list/all',
		dataType: 'json',
		success: function(data){
			var commentContent = '<li>全部仓库</li>';
			if(data) {
				for(var i=0; i<data.length; i++) {
					var comment = data[i];
					var date1 = comment.publishDate == null ? '' : comment.publishDate;
					commentContent += '<li data-id="'+comment.id+'">' + comment.warehouseName+'</li>';
				}
				$('#warehouseSearchBox').html(commentContent);
			}
		},
		error : function(data){
			layer.msg('仓库搜索条件加载失败', {time:500,icon:5});
		}
	});
}

/**
 * 加载出入库信息列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, type, orderCode, warehouseId, beginTime, endTime) {
	$.ajax({
		type: "POST",
		url: "/diconet/outbound/findInboundOrOutboundList",
		data: {
			page : page,
			rows : rows,
			type: type,
			orderCode: orderCode,
			warehouseId: warehouseId,
			beginTime: beginTime,
			endTime: endTime
		},
		dataType: 'json',
		success: function(data){
			console.log(data);
			var commentContent = '';
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<tr>';
					commentContent += '<td><img src="/docs/'+comment.productImgUrl+'" alt=""/></td>';// 商品图片
					commentContent += '<td><p>'+comment.productCode+'</p><p>'+comment.productName+'</p></td>';// 商品名称
					var htmlEle = "";
					var specValues = comment.productPriceId;
					var specValue = specValues.split(",");
					for(var num=0; num<specValue.length; num++){
						htmlEle += "<p>"+specValue[num]+"</p>"
					}
					commentContent += '<td>'+htmlEle+'</td>';// 规格
					commentContent += '<td>'+comment.inventoryQuantity+'</td>';// 所在仓库库存
					commentContent += '<td>'+comment.warehouseName+'</td>';// 仓库
					commentContent += '<td>'+comment.bigUnitName+comment.littleUnitName+'</td>';// 单位
					commentContent += '<td>'+comment.categoryName+'</td>';// 类型
					commentContent += '<td>'+comment.outboundQuantity+'</td>';// 数量
					commentContent += '<td>'+comment.outboundTime+'</td>';// 时间
					commentContent += '<td><button class="detail" onclick="detail(\''+comment.id+'\')" >详情</button></td>';
					commentContent += '</tr>';
					
				}
				$('.product-list-tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+type+'\',\''+orderCode+'\',\''+warehouseId+'\',\''+beginTime+'\',\''+endTime+'\');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}

/**
 * 出入商品详情
 */
function detail(id){
	window.location.href = "/DistributorSystem/warehouse/findOutInProductInfoById?id="+id;
}

init();
